#include <iostream>
#include <unistd.h>
#include <signal.h>

void PrintPending(sigset_t pending)
{
    std::cout<< getpid() <<" process pending: ";
    for(int signo=31; signo>0; --signo)
    {
        //该信号在pending中打印1，否则打印0
        if(sigismember(&pending, signo)){
            std::cout<< 1 << " ";
        } else{
            std::cout<< 0 << " ";
        }
    }
    std::cout<<std::endl;
}
void handler(int sigNum)
{
    std::cout<< "sigNum: " << sigNum <<std::endl; 
    sigset_t pending;
    while(true) 
    {
        //获取当前进程的pending
        sigpending(&pending);
        PrintPending(pending);
        sleep(2);
    }
}
int main()
{
    struct sigaction act, oldact;
    act.sa_handler = handler;

    //sa-mask所有比特位设置为0
    sigemptyset(&act.sa_mask);
    //给act中的sa_mask添加3号信号
    sigaddset (&act.sa_mask, 3);

    act.sa_flags = 0;

    //捕获2号信号
    sigaction(2, &act, &oldact);

    while(true)
    {
        std::cout<< "pid: " << getpid() <<std::endl;
        sleep(2);
    }

    return 0;
}
